<template>
  <main id="main" ref="main" :style="{width:'100%',height}"></main>
</template>

<script lang="js">
const echarts = require("echarts")
export default {
  name: "cBar",
  props: {
    barData: {
      type: Object
    },
    height: {
      type: String,
      default: "100%"
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    barData: {
      deep: true,
      handler() {
        this.initCarts();
      }
    }
  },
  mounted() {
    this.initCarts();
  },
  methods: {
    initCarts() {
      if (!this.chart) {
        this.chart = echarts.init(this.$refs['main']);
      }
      const option = {
        legend: {
          data: this.barData.dataList.map(item => item.series.name),
          top: "0%",
          left: '3%'
        },
        grid: {
          top: '5%',
          left: '0%',
          right: '0%',
          bottom: '0%',
          containLabel: true
        },
        yAxis: {
          type: 'category',
          data: this.barData.xAxis,
          position: 'left',
          axisLine: {show: false},
          splitArea: {show: false},
          minorSplitLine: {show: false}
        },
        xAxis: [
          {
            type: 'value',
            min: 0,
            max: 100,
            axisLabel: {
              formatter: '{value} %',
              textStyle: {
                align: 'right',
                color: '#484D78',
                fontSize: 12
              }
            }
          }
        ],
        series: this.barData.dataList.map(item => ({
          barWidth: 20,
          stack: 'one',
          label: {
            show: true,
            formatter: ({data}) => data !== 0 ? `${data}%` : ''
          },
          emphasis: {
            itemStyle: {
              barBorderWidth: 1,
              shadowBlur: 10,
              shadowOffsetX: 0,
              shadowOffsetY: 0,
              shadowColor: 'rgba(0,0,0,0.5)'
            }
          },
          data: item.data,
          ...item.series
        }))
      };
      this.chart.setOption(option);
    }
  }
}
</script>

<style scoped lang="stylus">

</style>
